package com.example.wuqujingdianphone.activity;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.wuqujingdianphone.R;
import com.example.wuqujingdianphone.base.BaseActivity;
import com.example.wuqujingdianphone.bean.LoginBean;
import com.example.wuqujingdianphone.bean.RegisterBean;
import com.example.wuqujingdianphone.utils.ToastUtil;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RegisterActivity extends BaseActivity {
    private static final String TAG = "MessageModifyActivity";
    @BindView(R.id.title)
    TextView mTitle;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;

    @BindView(R.id.ok)
    Button mOk;
    @BindView(R.id.loginname)
    EditText loginname;
    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.password_again)
    EditText passwordAgain;
    @BindView(R.id.realname)
    EditText realname;
    @BindView(R.id.nickname)
    EditText nickname;
    @BindView(R.id.phone)
    EditText phone;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regidter);
        ButterKnife.bind(this);

        mTitle.setText("注册");
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        initData();
    }

    private void initData() {

        //http://wenqujingdian.com/index.php/Jiekou/saveinf
        mOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isLegal()) {
                    ViseHttp.GET("index.php/jiekou/MemberSignup")
                            .tag(TAG)
                            .addParam("memberAccount", accout)
                            .addParam("memberPass", strpassword)
                            .addParam("realName", realName)
                            .addParam("nickName", strNickname)
                            .addParam("telphone", strPhone)
                            .request(new ACallback<RegisterBean>() {
                                @Override
                                public void onSuccess(RegisterBean result) {
                                    //Log.i(TAG, "onSuccess: result=" + result);
                                    if (result != null) {
                                        if (result.isResult()) {
                                            ToastUtil.show("注册成功，请登录");
                                            finish();
                                        }

                                    } else {
                                        ToastUtil.show("注册失败");
                                    }

                                }

                                @Override
                                public void onFail(int errCode, String errMsg) {
                                    //请求失败，errCode为错误码，errMsg为错误描述
                                    ToastUtil.showToast("errCode=" + errCode + " errMsg=" + errMsg);
                                }
                            });


                }

            }
        });


    }

    private String accout;
    private String strpassword;
    private String strpasswordAgain;
    private String strNickname;
    private String realName;
    private String strPhone;

    private boolean isLegal() {

        accout = loginname.getText().toString().trim();
        strpassword = password.getText().toString().trim();
        strpasswordAgain = passwordAgain.getText().toString().trim();
        strNickname = nickname.getText().toString().trim() + "";
        realName = realname.getText().toString().trim() + "";
        strPhone = phone.getText().toString().trim() + "";

        /*if (TextUtils.isEmpty(strUsername)) {
            ToastUtil.show("用户名不能为空");
            return false;
        }*/
        if (TextUtils.isEmpty(accout)) {
            ToastUtil.show("会员登录名不能为空");
            return false;
        }
        if (TextUtils.isEmpty(strpassword)) {
            ToastUtil.show("密码不能为空");
            return false;
        }

        if (TextUtils.isEmpty(strpasswordAgain)) {
            ToastUtil.show("确认密码不能为空");
            return false;
        }

        if (!strpassword.equals(strpasswordAgain)) {
            ToastUtil.show("两次密码输入不一致");
            return false;
        }


        return true;
    }


}
